import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:qm_line/constant.dart';
import 'package:qm_line/model/message.dart';

import 'qm_line_platform_interface.dart';
import 'model/response.dart';

/// An implementation of [QmLinePlatform] that uses method channels.
class MethodChannelQmLine extends QmLinePlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('qm_line');

  @override
  void addEventHandler(Future<dynamic> Function(MethodCall)? handler) {
    methodChannel.setMethodCallHandler(handler);
  }

  @override
  void switchServiceRoute(QMServiceLine serviceLine) {
    methodChannel.invokeMethod<Map<dynamic, dynamic>>(MethodKey.switchServiceRoute, {'serviceLine': serviceLine.index});
  }

  @override
  Future<QMResponse> initSDK(String accessId, String userName, String userId) async {
    final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>(MethodKey.initSDK, {'accessId': accessId, 'userName': userName, 'userId': userId});
    return QMResponse.fromMap(result);
  }

  @override
  void quitSDK() {
    methodChannel.invokeMethod<Map<dynamic, dynamic>>(MethodKey.quitSDK);
  }

  @override
  Future<QMResponse> beginChatSession(String peerId, Map<dynamic, dynamic> param) async {
    final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>(MethodKey.beginChatSession, {'peerId': peerId, 'param': param});
    return QMResponse.fromMap(result);
  }

  @override
  Future<QMResponse> sendTextMessage(String text) async {
    final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>(MethodKey.sendTextMessage, {'text': text});
    return QMResponse.fromMap(result);
  }

  @override
  Future<QMResponse> sendImageMessage(String imagePath) async {
    final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>(MethodKey.sendImageMessage, {'imagePath': imagePath});
    return QMResponse.fromMap(result);
  }

  @override
  Future<QMResponse> sendFileMessage(String filePath, String fileName, String fileSize) async {
    final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>(MethodKey.sendFileMessage, {'filePath': filePath, 'fileName': fileName, 'fileSize': fileSize});
    return QMResponse.fromMap(result);
  }

  @override
  Future<QMResponse> resendMessage(String messageId) async {
    final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>(MethodKey.resendMessage, {'messageId': messageId});
    return QMResponse.fromMap(result);
  }

  @override
  Future<QMResponse> leaveMessage(String peerId, String message) async {
    final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>(MethodKey.leaveMessage, {'peerId': peerId, 'message': message});
    return QMResponse.fromMap(result);
  }

  @override
  Future<QMResponse> getMessageList(int page) async {
    final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>(MethodKey.getMessageList, {'page': page});
    return QMResponse.fromMap(result);
  }

  @override
  Future<QMResponse> getMessage(String messageId) async {
    final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>(MethodKey.getMessage, {'messageId': messageId});
    return QMResponse.fromMap(result);
  }

  @override
  Future<QMResponse> deleteMessage(String messageId) async {
    final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>(MethodKey.deleteMessage, {'messageId': messageId});
    QMResponse message = QMResponse.fromMap(result);
    return message;
  }

  @override
  Future<QMResponse> getUnreadMessageCount(String accessId, String username, String userId) async {
    final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>(MethodKey.getUnreadMessageCount, {'accessId': accessId, 'userName': username, 'userId': userId});
    QMResponse message = QMResponse.fromMap(result);
    return message;
  }

  @override
  Future<QMResponse> getUnreadMessageList(String afterMsgId) async {
    final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>(MethodKey.getUnreadMessageList, {'afterMessageId': afterMsgId});
    return QMResponse.fromMap(result);
  }
}
